/// @tags: SegmentTree RangeHistoryMax
#include <algorithm>
#include <cstdio>
#include <iostream>

using namespace std;

namespace BlueQuantum {

typedef long long LL;

constexpr int MAXN = 5e5 + 3, INF = 0x3f3f3f3f;

struct Node {
  int l, r;
  int max, hismax, sec, mcnt;
  int maxadd, hismaxadd, nmaxadd, nhismaxadd;
  LL sum;
} tr[MAXN * 4];

int a[MAXN];

inline void pushUp(int x) {
  int ls = x << 1, rs = x << 1 | 1;
  tr[x].sum = tr[ls].sum + tr[rs].sum;
  tr[x].hismax = max(tr[ls].hismax, tr[rs].hismax);
  if (tr[ls].max == tr[rs].max) {
    tr[x].max = tr[ls].max;
    tr[x].sec = max(tr[ls].sec, tr[rs].sec);
    tr[x].mcnt = tr[ls].mcnt + tr[rs].mcnt;
  } else if (tr[ls].max > tr[rs].max) {
    tr[x].max = tr[ls].max;
    tr[x].sec = max(tr[ls].sec, tr[rs].max);
    tr[x].mcnt = tr[ls].mcnt;
  } else {
    tr[x].max = tr[rs].max;
    tr[x].sec = max(tr[ls].max, tr[rs].sec);
    tr[x].mcnt = tr[rs].mcnt;
  }
}

inline void update(int x, int maxadd, int hismaxadd, int nmaxadd, int nhismaxadd) {
  tr[x].sum += (LL)maxadd * tr[x].mcnt + (LL)nmaxadd * (tr[x].r - tr[x].l + 1 - tr[x].mcnt);
  tr[x].hismax = max(tr[x].hismax, tr[x].max + hismaxadd);
  tr[x].hismaxadd = max(tr[x].hismaxadd, tr[x].maxadd + hismaxadd);
  tr[x].max += maxadd, tr[x].maxadd += maxadd;
  tr[x].nhismaxadd = max(tr[x].nhismaxadd, tr[x].nmaxadd + nhismaxadd);
  if (tr[x].sec != -INF) tr[x].sec += nmaxadd;
  tr[x].nmaxadd += nmaxadd;
}

inline void pushDown(int x) {
  int ls = x << 1, rs = x << 1 | 1;
  int tmp = max(tr[ls].max, tr[rs].max);
  if (tr[ls].max == tmp)
    update(ls, tr[x].maxadd, tr[x].hismaxadd, tr[x].nmaxadd, tr[x].nhismaxadd);
  else
    update(ls, tr[x].nmaxadd, tr[x].nhismaxadd, tr[x].nmaxadd, tr[x].nhismaxadd);
  if (tr[rs].max == tmp)
    update(rs, tr[x].maxadd, tr[x].hismaxadd, tr[x].nmaxadd, tr[x].nhismaxadd);
  else
    update(rs, tr[x].nmaxadd, tr[x].nhismaxadd, tr[x].nmaxadd, tr[x].nhismaxadd);
  tr[x].maxadd = tr[x].hismaxadd = tr[x].nmaxadd = tr[x].nhismaxadd = 0;
}

void build(int x, int l, int r) {
  tr[x].l = l, tr[x].r = r;
  tr[x].maxadd = tr[x].hismaxadd = tr[x].nmaxadd = tr[x].nhismaxadd = 0;
  if (l == r) {
    tr[x].sum = tr[x].hismax = tr[x].max = a[l];
    tr[x].sec = -INF, tr[x].mcnt = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  pushUp(x);
}

void add(int x, int l, int r, int k) {
  if (tr[x].l > r || tr[x].r < l) return;
  if (l <= tr[x].l && tr[x].r <= r) return update(x, k, k, k, k);
  pushDown(x);
  add(x << 1, l, r, k), add(x << 1 | 1, l, r, k);
  pushUp(x);
}

void minimize(int x, int l, int r, int k) {
  if (tr[x].l > r || tr[x].r < l || k >= tr[x].max) return;
  if (l <= tr[x].l && tr[x].r <= r && k > tr[x].sec)
    return update(x, k - tr[x].max, k - tr[x].max, 0, 0);
  pushDown(x);
  minimize(x << 1, l, r, k), minimize(x << 1 | 1, l, r, k);
  pushUp(x);
}

LL querySum(int x, int l, int r) {
  if (tr[x].l > r || tr[x].r < l) return 0;
  if (l <= tr[x].l && tr[x].r <= r) return tr[x].sum;
  pushDown(x);
  return querySum(x << 1, l, r) + querySum(x << 1 | 1, l, r);
}

int queryMax(int x, int l, int r) {
  if (tr[x].l > r || tr[x].r < l) return -INF;
  if (l <= tr[x].l && tr[x].r <= r) return tr[x].max;
  pushDown(x);
  return max(queryMax(x << 1, l, r), queryMax(x << 1 | 1, l, r));
}

int queryHisMax(int x, int l, int r) {
  if (tr[x].l > r || tr[x].r < l) return -INF;
  if (l <= tr[x].l && tr[x].r <= r) return tr[x].hismax;
  pushDown(x);
  return max(queryHisMax(x << 1, l, r), queryHisMax(x << 1 | 1, l, r));
}

inline int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  build(1, 1, n);
  for (int l, r, k, opt; m; --m) {
    cin >> opt >> l >> r;
    switch (opt) {
      case 1:
        cin >> k;
        add(1, l, r, k);
        break;
      case 2:
        cin >> k;
        minimize(1, l, r, k);
        break;
      case 3:
        cout << querySum(1, l, r) << '\n';
        break;
      case 4:
        cout << queryMax(1, l, r) << '\n';
        break;
      case 5:
        cout << queryHisMax(1, l, r) << '\n';
        break;
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("SegmentTree3.in", "r", stdin);
  freopen("SegmentTree3.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
